export class KanaDataItem {
	constructor(
		public index: number,
		public row: number,
		public column: number,
		public romanji: string,
		public hiragana: string,
		public katakana: string
	) {}

	isPlaceholder() {
		return this.romanji === "x";
	}

	isValid() {
		return !this.isPlaceholder();
	}

	value() {
		return {
			row: this.row,
			index: this.index,
			column: this.column,
			romanji: this.romanji,
			hiragana: this.hiragana,
			katakana: this.katakana,
		};
	}
}

export function initKanaData() {
	if (
		!window.kana ||
		!window.kana.romanji ||
		!window.kana.hiragana ||
		!window.kana.katakana
	) {
		return "假名数据丢失";
	}

	if (
		window.kana.romanji.length !== window.kana.hiragana.length ||
		window.kana.romanji.length !== window.kana.katakana.length ||
		window.kana.hiragana.length !== window.kana.katakana.length
	) {
		return "假名数据长度未对齐";
	}
	const res: KanaDataItem[] = [];
	const count = window.kana.romanji.length;

	for (let i = 0; i < count; i++) {
		const column = i % 5;
		const row = Math.floor(i / 5);
		const kanaDataItem = new KanaDataItem(
			i,
			row,
			column,
			window.kana.romanji[i],
			window.kana.hiragana[i],
			window.kana.katakana[i]
		);

		res.push(kanaDataItem);
	}

	return res;
}
